import type { Rule } from '@/YmForm/interface/ValidateType';
import type {
  ButtonProps,
  DatePickerProps,
  InputNumberProps,
  InputProps,
  RadioGroupProps,
  SelectProps,
  TableColumnType,
  TableProps,
} from 'antd';
import type { CheckboxGroupProps } from 'antd/es/checkbox/Group';
import type { RangePickerProps } from 'antd/es/date-picker';
import type { FormInstance } from 'ym-design';

export type RecursivePartial<T> = NonNullable<T> extends object
  ? {
      [P in keyof T]?: T[P];
    }
  : T;

interface RFIOptionType {
  value: any;
  onChange: (val: any) => void;
}
export interface renderFormInputType<R = any> {
  (form: FormInstance, record: R, option: RFIOptionType): React.ReactElement;
}

export interface InputRangeProps
  extends Omit<InputProps, 'value' | 'onChange'> {
  value?: any[];
  onChange?: (newVal: any[]) => void;
}

export interface ValueTypeProps {
  type?:
    | 'input'
    | 'inputRange'
    | 'inputNumber'
    | 'date'
    | 'rangeDate'
    | 'select'
    | 'checkbox'
    | 'radio'
    | 'operation';
  inputProps?: InputProps;
  inputRangeProps?: InputRangeProps;
  inputNumberProps?: InputNumberProps;
  datePickerProps?: DatePickerProps;
  rangePickerProps?: RangePickerProps;
  checkboxGroupProps?: CheckboxGroupProps;
  radioGroupProps?: RadioGroupProps;
  selectProps?: SelectProps;
  operationProps?: {
    addBtn?: { hidden?: boolean } & ButtonProps;
    deleteBtn?: { hidden?: boolean } & ButtonProps;
  };
}

export interface YmEditTableColumnType<ReCordType = any>
  extends TableColumnType<ReCordType> {
  formItemProps?: {
    rules?: Rule[];
    trigger?: string;
    validateTrigger?: string | string[];
    valuePropName?: string;
  };
  valueType?: ValueTypeProps | renderFormInputType<ReCordType>;
  editable?: boolean;
  children?: YmEditTableColumnType<ReCordType>[];
}

export interface ActionRefType<R = any> {
  setRowsData: (rowData: R) => void;
  handleValidate: () => Promise<any>;
  handleReset: (currRowKey?: string) => void;
}

export interface YmEditTableProps<ReCordType = any>
  extends Omit<
    TableProps<ReCordType>,
    'columns' | 'pagination' | 'onChange' | 'rowKey'
  > {
  readonly?: boolean; // 是否开启只读模式，开启之后，将不可编辑，且隐藏操作列
  columns: YmEditTableColumnType<ReCordType>[];
  value?: ReCordType[];
  onRowValuesChange?: (rowIndex: number, newRecord: ReCordType) => void;
  onChange?: (newValue: ReCordType[]) => void;
  hideAddBtn?: boolean;
  max?: number; // 最多数据条数
  rowKey?: string;
  formInitialValues?: RecursivePartial<ReCordType>;
  dragAndDrop?: boolean;
}

export interface EditTableRowProps<ReCordType = any>
  extends React.HTMLAttributes<HTMLTableRowElement> {
  index: number;
  record: ReCordType;
  rowKey: string;
  fieldNames: string[];
  formInitialValues?: RecursivePartial<ReCordType>;
  onRowValuesChange?: (rowIndex: number, newRecord: ReCordType) => void;
  addFormInstance: (currRowKey: string, rowFormInstance: FormInstance) => void;
  removeFormInstance: (currRowKey: string) => void;
  dragAndDrop?: boolean;
  'data-row-key': string;
}

export interface EditTableCellProps<ReCordType = any> {
  editable?: boolean;
  record: ReCordType;
  rowIndex: number;
  formItemProps?: YmEditTableColumnType['formItemProps'];
  valueType?: ValueTypeProps | renderFormInputType<ReCordType>;
  children: React.ReactNode;
}

export interface FormItemProps<ReCordType = any> {
  formItemProps?: YmEditTableColumnType['formItemProps'];
  valueType?: ValueTypeProps | renderFormInputType<ReCordType>;
  form: FormInstance;
  record: ReCordType;
}
